home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / cleanhex.pqs / cleanhex.pas
Encoding:
Pascal/Delphi Source File  |  1986-03-13  |  1.3 KB  |  42 lines

  1. program CleanHex;
  2.  
  3. { two real short functions for converting hex chars to integer and vice versa }
  4. { Scott Martin, The Forbin Project, Minneapolis, Mn. }
  5.  
  6. const
  7.    HexC : string [16] = '0123456789ABCDEF';
  8.  
  9. type
  10.    str04 = string [04];
  11.  
  12. var
  13.    x : integer;
  14.    hexstring : str04;
  15.  
  16. { convert upper case hex input string, 4 chars long, to a 2-byte integer }
  17. { edit the input before using HexCon! }
  18. function HexCon (instr : str04) : integer;
  19.    begin
  20.       HexCon := pred(pos(instr[1],HexC)) shl 12 +
  21.                 pred(pos(instr[2],HexC)) shl 8  +
  22.                 pred(pos(instr[3],HexC)) shl 4  +
  23.                 pred(pos(instr[4],HexC));
  24.    end;
  25.  
  26. { convert an integer to its 4-byte hex representation }
  27. function Hex4 (n : integer) : str04;
  28.    begin
  29.       Hex4 := copy (HexC,succ(hi (n) shr 4), 1) +
  30.               copy (HexC,succ(hi (n) and 15),1) +
  31.               copy (HexC,succ(lo (n) shr 4), 1) +
  32.               copy (HexC,succ(lo (n) and 15),1);
  33.    end;
  34.  
  35. begin
  36.    repeat
  37.       write ('Enter an integer ');
  38.       readln (x);
  39.       hexstring := hex4(x);
  40.       writeln ('Hex representation = ',hexstring,' Integer = ',HexCon (hexstring));
  41.    until x = 0;
  42. end.